package com.zdzx.controller.admin.base;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.SystemConfigMapper;
import com.zdzx.model.service.base.vo.SystemConfig;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "系统配置")
@RestController
@RequestMapping("/admin")
public class SystemConfigController {
	
	@Autowired
	private SystemConfigMapper systemConfigMapper;
	@ApiOperation(value = "获取类型列表", notes = "") 
	@GetMapping("/SystemConfig") 
	public Result<List<SystemConfig>> list(){
		List<SystemConfig> list = systemConfigMapper.selectList(null);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, list);
	}
	
	@ApiOperation(value = "获取类型列表", notes = "") 
	@GetMapping("/SystemConfig/{key}") 
	public Result<SystemConfig> queryKey(@PathVariable("key") String key){
		QueryWrapper<SystemConfig> queryWrapper=new QueryWrapper<SystemConfig>();
		queryWrapper.eq("key", key);
		List<SystemConfig> list = systemConfigMapper.selectList(queryWrapper);
		SystemConfig systemConfig=new SystemConfig();
		if(list!=null&&list.size()>0) {
			systemConfig = list.get(0);
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, systemConfig);
	}
	
	
	@ApiOperation(value = "设置类型", notes = "") 
	@PostMapping("/systemConfig/{key}/{value}") 
	public Result<SystemConfig> setSystemConfig(@PathVariable("key") String key,@PathVariable("value")String value){
		UpdateWrapper<SystemConfig> updateWrapper=new UpdateWrapper<SystemConfig>();
		 updateWrapper.set("`value`", value);
		 updateWrapper.eq("`key`",key);
		int update = systemConfigMapper.update(null, updateWrapper);
		return ResultUtil.data(update>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
	}
	
	

}
